<template>
  <div class="permset">
    <div class="header">
      <span>角色管理</span>
    </div>
    <div class="line"></div>
    <div class="main">
      <el-form-item style="margin: 10px">
        <el-button
          type="warning"
          @click="dialogFormVisible = true"
          style="width: 150px; height: 40px"
          >+新增角色</el-button
        >
      </el-form-item>
      <el-dialog v-model="dialogFormVisible" :title="title">
        <el-form :model="form">
          <el-form-item label="角色名称">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="所属部门">
            <el-select v-model="form.region" placeholder="请选择">
              <el-option label="管理部门" value="管理部门" />
              <el-option label="销售部门" value="销售部门" />
              <el-option label="产品部门" value="产品部门" />
            </el-select>
          </el-form-item>
          <el-form-item label="角色简介">
            <el-input v-model="form.resource" autocomplete="off" />
          </el-form-item>
          <el-form-item label="是否启用">
            <el-checkbox-group v-model="form.checkList">
              <el-checkbox label="是" />
              <el-checkbox label="否" />
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="handleadd"> 确定 </el-button>
          </span>
        </template>
      </el-dialog>
      <div class="table">
        <el-table 
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="序号" width="180" />
          <el-table-column prop="name" label="用户角色" width="180" />
          <el-table-column prop="miaoshu" label="角色描述" width="180" />
          <el-table-column prop="bumen" label="所属部门" width="180" />
          <el-table-column prop="use" label="是否启用" width="180" />
          <el-table-column prop="time" label="操作时间" width="180" />
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="handledel(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                link
                type="primary"
                size="small"
                @click="handleRemove(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox, ElMessage } from "element-plus";
import { Checked } from "@element-plus/icons-vue";
import dayJS from "dayjs";
import useManagementStore from "../../store/management";
import { ref, reactive } from "vue";
import { fromPairs } from "lodash";
const title = ref("新增角色");
const dialogFormVisible = ref(false);
const store = useManagementStore();
const managements = store.managements;
const tableData = ref([]);
tableData.value = managements;
const form = reactive({
  name: "",
  region: "",
  resource: "",
  checkList: ["是", "否"],
});
// 编辑
const handledel = (id) => {
  title.value = "编辑角色";
  dialogFormVisible.value = true;
  const result = store.delMans(id);
  form.name = result.name;
  form.region = result.bumen;
  form.resource = result.miaoshu;
};
// 增加
const handleadd = () => {
  title.value = "新增角色";
  dialogFormVisible.value = false;
  const add = reactive({
    id: parseInt(Math.random() * 10000000),
    name: form.name,
    miaoshu: form.resource || "暂无",
    bumen: form.region || "暂无",
    use: form.checkList[0],
    time: dayJS(new Date()).format("YYYY年MM月DD日"),
  });
  if (form.name) {
    store.addMans(add);
  } else {
    ElMessage.error("角色名必填");
  }

  form.name = "";
  form.region = "";
  form.resource = "";
};
// 删除

const handleRemove = (id) => {
  ElMessageBox.confirm("是否删除当前数据?", "删除提示", {
    confirmButtonText: "是",
    cancelButtonText: "否",
    type: "warning",
  }).then(() => {
    store.removeMans(id);
  });
};
</script>

<style lang="less" scoped>
:deep(.el-dialog__header) {
  text-align: center;
  background-color: rgb(255, 153, 0);
  width: 94.8%;
}
:deep(.el-dialog__body) {
  padding: 60px;
}
.header {
  width: 100%;
  height: 40px;
  padding: 5px;
  color: rgb(255, 153, 0);
  line-height: 30px;
  span {
    display: inline-block;
    width: 100px;
    height: 26px;
    border-left: 6px solid rgb(255, 153, 0);
    padding: 5px;
  }
}
.table {
  margin-top: 20px;
}
.line {
  height: 10px;
  width: 100%;
  background-color: rgb(246, 239, 239);
}
.main {
  padding: 10px;
  .btn {
    color: #fff;
    background-color: rgb(255, 153, 0);
    border: none;
    width: 140px;
    height: 40px;
    cursor: pointer;
  }
}
</style>
